package com.shoujifeng.snowmusic.player;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.shoujifeng.snowmusic.adapter.DownloadOverAdapter;
import com.shoujifeng.snowmusic.db.DatabaseUtil;
import com.shoujifeng.snowmusic.entity.Mp3Info;
import com.shoujifeng.snowmusic.player.http.ServerAccess;
import com.shoujifeng.snowmusic.player.interfaces.MoreInterface;
import com.shoujifeng.snowmusic.player.utils.MusicPlayer;

public class DownLoadOverActivity extends Activity {
	private ListView listView;
	private DownloadOverAdapter mAdapter;
	private View lineLay;
	private LinearLayout EditLay;
	public static ArrayList<HashMap<String, Object>> mDataList;
	private int mPage = 0;
	private List<Mp3Info> downMusicInfos;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.download_over);
		init();
	}

	
	@Override
	protected void onResume() {
		setListView();
		super.onResume();
	}


	public void init() {
		listView = (ListView) findViewById(R.id.download_over_list);
		listView.setDivider(null);
		lineLay = LayoutInflater.from(DownLoadOverActivity.this).inflate(R.layout.line_layout, null);
		listView.addFooterView(lineLay);
		listView.setOnItemClickListener(itemClickListener);
		EditLay = (LinearLayout) findViewById(R.id.native_edit_lay);
		EditLay.setOnClickListener(onClickListener);
		
	}
	private View.OnClickListener onClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.native_edit_lay:
				toEdit();
				break;

			default:
				break;
			}
			
		}
	};
	
	private void toEdit() {
		downMusicInfos = new ArrayList<Mp3Info>();
		
		for(int i = 0;i<mDataList.size();i++){
			HashMap<String, Object>  map = new HashMap<String, Object>();
			map = mDataList.get(i);
			Mp3Info mp3Info = new Mp3Info();
			
				mp3Info.setTitle(map.get(ServerAccess.MUSIC_NAME).toString());
				mp3Info.setArtist(map.get(ServerAccess.SINGER).toString());
				mp3Info.setUrl(map.get(ServerAccess.FILE_URL).toString());
				mp3Info.setId(Integer.parseInt(map.get(ServerAccess.UID).toString()));
				
			downMusicInfos.add(mp3Info);
		}
		
		MusicPlayer.musicInfos = downMusicInfos;
		
		
		Intent intent = new Intent(DownLoadOverActivity.this,EditListMusicActivty.class);
		intent.putExtra("activityName", "DownLoadOverActivity");
		startActivity(intent);
		overridePendingTransition(R.anim.right_in, R.anim.left_out);
	}
	
	private OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> adapterView, View view, int position,
				long arg3) {
			
			//播放本地歌曲
			downMusicInfos = new ArrayList<Mp3Info>();
			
			for(int i = 0;i<mDataList.size();i++){
				HashMap<String, Object>  map = new HashMap<String, Object>();
				map = mDataList.get(i);
				Mp3Info mp3Info = new Mp3Info();
				
					mp3Info.setTitle(map.get(ServerAccess.MUSIC_NAME).toString());
					mp3Info.setArtist(map.get(ServerAccess.SINGER).toString());
				//	mp3Info.setUrl(map.get(new String(ServerAccess.FILE_URL).getBytes("GB2312"),"ISO-8859-1").toString());
					String url = map.get(ServerAccess.FILE_URL).toString();
					
					mp3Info.setUrl(url);
				downMusicInfos.add(mp3Info);
			}
			
			MusicPlayerActivity.isOnline = false;
			MusicPlayer.musicInfos = downMusicInfos;
			MusicPlayer.position = position ;
			MusicPlayer.PLAY_ORDER =1;
			Intent intent = new Intent(DownLoadOverActivity.this,MusicPlayerActivity.class);
			startActivity(intent);
			
			overridePendingTransition(R.anim.bottom_in,android.R.anim.fade_out);
		}
	};
	

	private void setListView() {
		
		DatabaseUtil databaseUtil = new DatabaseUtil(this);
		mDataList = databaseUtil.getDownload(mPage, 1);
		
		mAdapter = new DownloadOverAdapter(this, mDataList);
		listView.setAdapter(mAdapter);
		
		mAdapter.addInterfaceOnMore(moreInterface);
	}
	
	private MoreInterface moreInterface = new MoreInterface() {
		
		@Override
		public void OnClickDownload(int position) {
			// TODO Auto-generated method stub
			
			//实际为加入歌单
			HashMap<String, Object>  map = new HashMap<String, Object>();
			map = mDataList.get(position);
			map.put("title", map.get(ServerAccess.MUSIC_NAME).toString());
			map.put("Artist", map.get(ServerAccess.SINGER).toString());
			map.put("url",  map.get(ServerAccess.FILE_URL).toString());
			map.put("duration", 0);
			map.put("size", 0);
			map.put("album", "");
			EditListMusicActivty.mp3list = new ArrayList<HashMap<String, Object>>();
			EditListMusicActivty.mp3list.add(map);
			
			EditListMusicActivty.noList = new ArrayList<String>();
			EditListMusicActivty.noList.add(0 + "");
			
//			 musicMap.put("title",musicName);
//			 musicMap.put("Artist",playerName);
//			 musicMap.put("duration",duration);
//			 musicMap.put("size",size);
//			 musicMap.put("url",url);
			
			ServerAccess.AddOneMusicToMenu = true;
			ServerAccess.musicMap.put("title",  map.get(ServerAccess.MUSIC_NAME).toString());
			ServerAccess.musicMap.put("Artist",  map.get(ServerAccess.SINGER).toString());
			ServerAccess.musicMap.put("url",  map.get(ServerAccess.FILE_URL).toString());
			ServerAccess.musicMap.put("duration",  0);
			ServerAccess.musicMap.put("size",  0);
			Intent intent = new Intent(DownLoadOverActivity.this,EditAddTo.class);
			startActivity(intent);
			
			overridePendingTransition(R.anim.right_in, R.anim.left_out);
		}
		
		@Override
		public void OnClickConllect(int position, Boolean have) {
			// TODO Auto-generated method stub
			dialog(position, have);
		}
	};
	
    protected void dialog(final int position, Boolean have) { 
        AlertDialog.Builder builder = new Builder(DownLoadOverActivity.this); 
        builder.setMessage("确定要删除该歌曲吗?"); 
        builder.setTitle("提示"); 
        builder.setPositiveButton("删除", 
                 
                new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						DatabaseUtil databaseUtil = new DatabaseUtil(DownLoadOverActivity.this);
						databaseUtil.deleteDown(mDataList.get(position).get("name").toString());
						mDataList.remove(position);
						mAdapter.notifyDataSetChanged();
						Toast.makeText(DownLoadOverActivity.this, "删除成功!", Toast.LENGTH_LONG).show();
						//实际为删除歌曲
					}
        	
        }); 
        builder.setNegativeButton("取消", null); 
        builder.create().show(); 
    } 
 
}
